﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework;
using OLTS.DAL;
using OLTS.MODEL;

namespace OLTS.DATA_SERVICE
{
    public class EMERGENCY_NOTIFY_Service
    {
        private OTS_EMERGENCY_NOTIFY _objApp = new OTS_EMERGENCY_NOTIFY();

        public EMERGENCY_NOTIFY_Service(OTS_EMERGENCY_NOTIFY objApp)
        {
            _objApp = objApp;
        }

        public EMERGENCY_NOTIFY_Service()
        {

        }

        public List<OTS_EMERGENCY_NOTIFY> GetAll()
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_EMERGENCY_NOTIFY.ToList();
                return list;
            }
        }
        public List<OTS_EMERGENCY_NOTIFY> GetByCurentLogin(string currentuser)
        {
            using (var ctx = new Entities())
            {
                var list = ctx.OTS_EMERGENCY_NOTIFY.Where(x => x.EMPLOYEE_ID == currentuser).ToList();
                return list;
            }
        }
        public void GetAllinclude()
        {

        }

        public void Add(List<OTS_EMERGENCY_NOTIFY> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Create)
                {
                    var model = new OTS_EMERGENCY_NOTIFY();
                    model.EMPLOYEE_ID = obj.EMPLOYEE_ID;
                    model.NAME = obj.NAME;
                    model.RELATIONSHIP = obj.RELATIONSHIP;
                    model.ADDRESS = obj.ADDRESS;
                    model.MOBILE = obj.MOBILE;
                    using (var ctx = new Entities())
                    {
                        ctx.OTS_EMERGENCY_NOTIFY.AddObject(model);
                        ctx.SaveChanges();
                    }
                }
            }
        }

        public void Edit(List<OTS_EMERGENCY_NOTIFY> list)
        {
            foreach (var obj in list)
            {
                if (obj.Action == ActionEnum.Update)
                {
                    using (var ctx = new Entities())
                    {
                        var objEdit = ctx.OTS_EMERGENCY_NOTIFY.Where(x => x.ID == obj.ID).FirstOrDefault();
                        if (objEdit != null)
                        {
                            objEdit.EMPLOYEE_ID = obj.EMPLOYEE_ID;
                            objEdit.NAME = obj.NAME;
                            objEdit.RELATIONSHIP = obj.RELATIONSHIP;
                            objEdit.ADDRESS = obj.ADDRESS;
                            objEdit.MOBILE = obj.MOBILE;
                            ctx.SaveChanges();
                        }
                    }
                }
            }
        }

        public void Delete()
        {
            using (var ctx = new Entities())
            {
                var obj = ctx.OTS_EMERGENCY_NOTIFY.Where(x => x.ID == _objApp.ID).FirstOrDefault();
                if (obj != null)
                {
                    ctx.OTS_EMERGENCY_NOTIFY.DeleteObject(obj);
                    ctx.SaveChanges();
                }

            }

        }

    }
}
